package RestaurantManagement;

import java.util.ArrayList;
import java.util.HashMap;

public class RestaurantService {
    private HashMap<String, Ingredient> ingredientInventory = new HashMap<>();
    private ArrayList<Meal> mealsList = new ArrayList<>();
    private RestaurantBilling billing = new RestaurantBilling();
    private HashMap<String, HashMap<String, Integer>> orders = new HashMap<>();
    private int orderCounter = 1;

    public void addIngredient(String name, double price){
        if (ingredientInventory.containsKey(name)) {
            System.out.println("An ingredient with this name already exists.");
            return;
        }

        if (price < 0) {
            System.out.println("Price must be a positive number.");
            return;
        }

        Ingredient newIngredient = new Ingredient(name, price);
        ingredientInventory.put(name, newIngredient);
        System.out.println("Ingredient '" + name + "' added successfully.");
    }

    public void createMeal(String mealName, ArrayList<String> ingredientNames){
        if (ingredientInventory.isEmpty()) {
            System.out.println("No ingredients available. Please add ingredients first.");
            return;
        }

        Meal meal = new Meal(mealName);

        for (String ingredientName : ingredientNames) {
            Ingredient ingredient = ingredientInventory.get(ingredientName);
            if (ingredient != null) {
                meal.addIngredient(ingredient);
            } else {
                System.out.println("Ingredient '" + ingredientName + "' not found and skipped.");
            }
        }

        if (!meal.getIngredients().isEmpty()) {
            mealsList.add(meal);
            billing.addMeal(meal);
            System.out.println("Meal '" + mealName + "' created successfully.");
        } else {
            System.out.println("Meal creation failed - no valid ingredients were added.");
        }
    }

    public String placeOrder(HashMap<String, Integer> mealQuantities){
        // Clear any existing items in the billing system
        billing.clearOrder();

        // Add meals to the order
        for (String mealName : mealQuantities.keySet()) {
            int quantity = mealQuantities.get(mealName);
            for (int i = 0; i < quantity; i++) {
                billing.addMealToOrder(mealName);
            }
        }

        // Generate order ID and store the order
        String orderId = "ORD-" + orderCounter++;
        orders.put(orderId, new HashMap<>(mealQuantities));

        return orderId;
    }

    public double calculateBill(String orderId){
        if (!orders.containsKey(orderId)) {
            System.out.println("Order ID not found.");
            return 0;
        }
        double total = 0;
        HashMap<String, Integer> mealQuantities = orders.get(orderId);
        for (String mealName : mealQuantities.keySet()) {
            int quantity = mealQuantities.get(mealName);
            Meal meal = billing.findMealByName(mealName);
            if (meal != null) {
                total += meal.getPrice() * quantity;
            }
            else {
                System.out.println("Meal '" + mealName + "' not found in menu, skipping.");
            }
        }
        return total;
    }

    public Meal findMealByName(String mealName) {
        for (Meal meal : mealsList) {
            if (meal.getName().equalsIgnoreCase(mealName)) {
                return meal;
            }
        }
        return null;
    }
}